package com.sinogrand.elevator.repository

import android.content.Context
import android.util.Log
import com.sinogrand.elevator.data.models.ElevatorListResponse
import com.sinogrand.elevator.data.models.ElevatorDetailData
import com.sinogrand.elevator.data.models.ElevatorLogListResponse
import com.sinogrand.elevator.network.NetworkClient
import com.sinogrand.elevator.utils.LogTags
import com.sinogrand.elevator.utils.PaginationUtils
import com.google.gson.JsonObject

/**
 * 电梯仓库类
 */
class ElevatorRepository(context: Context) {
    
    private val apiService = NetworkClient.getApiService(context)
    
    /**
     * 获取项目电梯列表
     */
    suspend fun getProjectElevatorList(
        projectId: Int,
        page: Int = 1,
        size: Int = 20,
        no: String? = null
    ): Result<ElevatorListResponse> {
        return try {
            val request = JsonObject().apply {
                addProperty("project_id", projectId)
                addProperty("page", page)
                addProperty("size", size)
                if (no != null) {
                    addProperty("no", no)
                }
            }
            val apiResponse = apiService.getProjectElevatorList(request)
            
            if (apiResponse.code == 0 && apiResponse.data != null) {
                val pageInfo = apiResponse.data.page_info
                
                Log.i(LogTags.API, "✅ 获取项目电梯列表成功 - 项目ID: $projectId, 总数: ${pageInfo.total}, 当前页: ${pageInfo.page}, 返回: ${apiResponse.data.list.size}条")
                Result.success(apiResponse.data)
            } else {
                Log.e(LogTags.API, "❌ 获取项目电梯列表失败 - code: ${apiResponse.code}, msg: ${apiResponse.msg}")
                Result.failure(Exception(apiResponse.msg))
            }
        } catch (e: Exception) {
            Log.e(LogTags.API, "💥 获取项目电梯列表异常 - ${e.javaClass.simpleName}: ${e.message}")
            Result.failure(e)
        }
    }
    
    /**
     * 获取电梯详情
     */
    suspend fun getElevatorDetail(elevatorId: Int): Result<ElevatorDetailData> {
        return try {
            val request = JsonObject().apply {
                addProperty("id", elevatorId)
            }
            val apiResponse = apiService.getElevatorInfo(request)
            
            if (apiResponse.code == 0 && apiResponse.data != null) {
                Log.i(LogTags.API, "✅ 获取电梯详情成功 - 电梯ID: $elevatorId, 名称: ${apiResponse.data.info.name}")
                Result.success(apiResponse.data)
            } else {
                Log.e(LogTags.API, "❌ 获取电梯详情失败 - code: ${apiResponse.code}, msg: ${apiResponse.msg}")
                Result.failure(Exception(apiResponse.msg))
            }
        } catch (e: Exception) {
            Log.e(LogTags.API, "💥 获取电梯详情异常 - ${e.javaClass.simpleName}: ${e.message}")
            Result.failure(e)
        }
    }
    
    /**
     * 获取电梯列表（通用）
     */
    suspend fun getElevatorList(
        page: Int = 1,
        pageSize: Int = 20,
        keyword: String? = null
    ): Result<ElevatorListResponse> {
        return try {
            val request = JsonObject().apply {
                addProperty("page", page)
                addProperty("pageSize", pageSize)
                if (keyword != null) {
                    addProperty("keyword", keyword)
                }
            }
            val apiResponse = apiService.getElevatorList(request)
            
            if (apiResponse.code == 0 && apiResponse.data != null) {
                val pageInfo = apiResponse.data.page_info
                
                Log.i(LogTags.API, "✅ 获取电梯列表成功 - 总数: ${pageInfo.total}, 当前页: ${pageInfo.page}/${PaginationUtils.getTotalPages(pageInfo)}, 返回: ${apiResponse.data.list.size}条")
                Result.success(apiResponse.data)
            } else {
                Log.e(LogTags.API, "❌ 获取电梯列表失败 - code: ${apiResponse.code}, msg: ${apiResponse.msg}")
                Result.failure(Exception(apiResponse.msg))
            }
        } catch (e: Exception) {
            Log.e(LogTags.API, "💥 获取电梯列表异常 - ${e.javaClass.simpleName}: ${e.message}")
            Result.failure(e)
        }
    }
    
    /**
     * 获取电梯日志列表
     */
    suspend fun getElevatorLogList(
        elevatorId: Int,
        type: Int,
        page: Int = 1,
        size: Int = 20
    ): Result<ElevatorLogListResponse> {
        return try {
            val request = JsonObject().apply {
                addProperty("id", elevatorId)
                addProperty("type", type)
                addProperty("page", page)
                addProperty("size", size)
            }
            val apiResponse = apiService.getElevatorLogList(request)
            
            if (apiResponse.code == 0 && apiResponse.data != null) {
                val pageInfo = apiResponse.data.page_info
                
                Log.i(LogTags.API, "✅ 获取电梯日志列表成功 - 电梯ID: $elevatorId, 类型: $type, 总数: ${pageInfo.total}, 当前页: ${pageInfo.page}, 返回: ${apiResponse.data.list.size}条")
                Result.success(apiResponse.data)
            } else {
                Log.e(LogTags.API, "❌ 获取电梯日志列表失败 - code: ${apiResponse.code}, msg: ${apiResponse.msg}")
                Result.failure(Exception(apiResponse.msg))
            }
        } catch (e: Exception) {
            Log.e(LogTags.API, "💥 获取电梯日志列表异常 - ${e.javaClass.simpleName}: ${e.message}")
            Result.failure(e)
        }
    }
}